package com.ych.modules.cms.unAuthorInfc.login.controller;

import com.ych.modules.api.unAuthorInfc.login.entity.LoginVo;
import com.ych.modules.common.service.ICaptchaService;
import com.ych.modules.cms.unAuthorInfc.login.service.ICmsUserInfoService;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/19
 * Time: 14:24
 * Description:
 */
@RestController("cmsLoginController")
@RequestMapping("/cms/unAuth/login")
@Api(tags = {"CMS——登录相关"}, description = "CMS——登录相关", position = 2)
public class LoginController {
    @Autowired
    private ICmsUserInfoService iCmsUserInfoService;
    @Autowired
    private ICaptchaService     iCaptchaService;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public R<Map<String, Object>> login(@RequestBody LoginVo loginVo) {
        try {
            iCaptchaService.validate(loginVo.getUuid(), loginVo.getCode());
            Map<String, Object> map = iCmsUserInfoService.login(loginVo);
            return new R(0, "请求成功", map);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "图片验证码", position = 1)
    @GetMapping("/captcha.jpg")
    public void captcha(HttpServletResponse response, String uuid) {
        iCaptchaService.getCaptcha(response, uuid);
    }

}
